/*
 * Copyright (C) 2013 Fabiano Tarlao
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.easybenchmark.client.benchmarks;

import com.google.gwt.regexp.shared.MatchResult;
import com.google.gwt.regexp.shared.RegExp;


/**
 *
 * @author Fabiano Tarlao
 */
public class RegExBenchmark extends  Benchmark{
    private String destString = new String();
    private String brick="segreteriaeconomia@uniba.it  The Beginning 1 In the segreteria.economia@uniba.it beginning God created the heavens segreteria.economia@uniba.it and the earth. 2 Now the earth was a formless and empty, "
            + "darkness was over the surface of the deep, and the Spirit of God was segreteria.economia@uniba.it hovering over the waters. 3 And God said, Let there be light, "
            +"dal lunedì al venerdì: 10.00 - 12.00 martedì e giovedì: 15.00 - 17.00     Facoltà di Agraria ampus universitario - Via Amendola, 165/a - Bari  e-mail: segreteriastudenti.agraria@agr.uniba.it  tel. 080 544 3101 "
            + " fax 080 544 3100     Facoltà di Economia Via Camillo Rosalba, 53 - Bari -mail: segreteria.economia@uniba.it  el. 080 504 9166 - 9165 - 9164 fax 080 504 9330      II Facoltà di Economia "
            + " alazzo D'Aquino - Pendio Lariccia - Taranto  e-mail: v.spasiano@ssta1.uniba.it tel. 099 471 7643 - 7619 7620  fax 099 471 7647      Facoltà di Farmacia Campus universitario - Via Orabona, 4 - Bari";
            
    private int blockSize=brick.length();
    private int WARMUP_TEXT_SIZE =60;
    private int DEFAULT_TEXT_SIZE=100;
    private int benchmarkTextSize;
    private int currentBenchTextSize;
    private String regexPattern="[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}";
    //"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\\b";

    public RegExBenchmark() {
        this.benchmarkTextSize = DEFAULT_TEXT_SIZE;
    }

    public RegExBenchmark(int benchmarkTextSize) {
        this.benchmarkTextSize = benchmarkTextSize;
    }
    
    
    private String textBlock;
    private RegExp regExp = RegExp.compile(regexPattern);
    
    private String createTextBlock(int size){
        StringBuilder textBlockGrow = new StringBuilder();
        while(textBlockGrow.length()<size){
            textBlockGrow.append(brick);
        }
        return textBlockGrow.toString();
    }
    
    @Override
    protected int initWarmUpTask() {
        currentBenchTextSize=WARMUP_TEXT_SIZE;
        textBlock=createTextBlock(currentBenchTextSize);
        return textBlock.length();
    }

    @Override
    protected int initBenchTask() {
       currentBenchTextSize=benchmarkTextSize;
       textBlock=createTextBlock(currentBenchTextSize);
       return textBlock.length();
    }

    @Override
    protected long benchTask() {
      destString="";  
      long currentWorkDone=0;
      // Compile and use regular expression
        //MainEntryPoint.logLabel.setText("benchText start");
        String newText = new String(textBlock); //avoid optimizations
        MatchResult matcher = this.regExp.exec(newText);
        //MainEntryPoint.logLabel.setText("matcher");
        boolean matchFound = (matcher != null); // equivalent to regExp.test(inputStr); 
        currentWorkDone = (matchFound? matcher.getGroupCount() : 0)+textBlock.length(); //they are proportional, matches are so little, this operation is useful to avoid optimizations
        //MainEntryPoint.logLabel.setText("current work calculus");
        if( matchFound){
            destString = matcher.getGroup(0);
        }
      return currentWorkDone;
    }

    public String getDestString() {
        return destString;
    }
    
}
